March 1996

Customizing the vi editor

By Al Alexander

For years, UNIX users and administrators have had a love/hate relationship with the vi editor--in general, they've loved to hate it. Many of the problems people have with the vi editor can be alleviated with a few configuration options that can automatically be set during startup.

Most users never learn about this startup option and how much easier to use it can make the vi editor. In this article, we'll explore some of the most useful commands you can use to customize vi.

The vi startup sequence

When you start the vi editor, it searches for the environment variable EXINIT and uses its contents as a set of configuration commands. The EXINIT variable, however, is limited in the commands you can place in it and, as such, isn't the best option to use.

If EXINIT is not defined, vi looks for the .exrc file in your home directory (i.e., ~/.exrc) and uses its configuration commands. If one of the configuration commands in ~/.exrc is the :set exrc command, vi looks in your current directory for a file named .exrc, and if it exists, vi executes the commands in that file.

In this manner, you can have a default configuration for vi as well as a different configuration for each directory you're working in. In this article, we'll modify the .exrc file in our home directory, which will affect all of our vi editing sessions.

Which customizations will make using vi simpler?

It's nice to know that iv provides such a flexible startup procedure. But for now, that's begging the question. Just what sorts of customizations does vi provide for?

It turns out that there are quite a few things you can do to make vi simpler to use. Some commands modify the way vi operates, and other commands allow you to create useful macros. We'll show a sampling of the more useful commands we've found.

Showing the current mode

One of the biggest complaints about vi is that you never know what mode you're in-there's nothing onscreen to indicate whether you're in command mode or insert mode. You can easily cure this complaint by issuing the :set showmode command.

The :set showmode command forces vi to give a visual indication of what mode vi is in when you're typing--insert mode, append mode, etc. If you're in command mode, as when you first enter vi, you won't see anything different. But as soon as you enter insert mode, the words INSERT MODE appear in the lower-right corner on your screen. Similarly, vi displays APPEND MODE, CHANGE MODE, and OPEN MODE when appropriate.

Masking screen garbage

When you're in a full-screen editor, one of the last things you want is another user or process sending messages to your screen. (Another user can use the talk command to ask you a question, for example.) When messages are sent to your screen, they're mixed with the vi screen, making it difficult to see what you're editing.

Hence, many vi users learn (and frequently use) the ^L command, which redraws the screen. Since the ^L command is short and simple, many people use it each time their screen is jumbled instead of searching for a better way. The better way is to tell vi not to allow other users or processes to send you messages. That is precisely what the :set nomsg command does.

Setting tab stops

By default, vi sets tab stops to eight characters. When you're programming, this can be annoying, as deeply-indented structures can scroll off the right side of the page. That's why programmers often use tab stops of three or four characters. With the smaller tab stops, you still get good indentation, but you don't run out of space so quickly.

You can tell vi how many characters wide a tab stop should be with the :set tab stop=# command. All you need to do is replace the # symbol with the number of character positions you'd like to use. Thus, for programming, you might use:

:set tab stop=4

Showing line numbers

Another feature that helps programmers is knowing the line number that you're on. When you compile a program and encounter errors, the compiler tells you which line numbers the errors are on so you can find them more quickly.

You can tell vi to display the line numbers in a source file by using the :set number command. When you want to turn line numbering off, simply use the :set nonumber command.

The EXINIT environment variable

If you use the EXINIT environment variable to customize vi, you're limited to using the set commands. (If you want to see a list of all the set commands, invoke vi and type :set all. When you do, vi will show you a list of all available set commands.)

In order to use the EXINIT method of customizing vi, you simply set the EXINIT environment variable with a string that represents the options you want. The first word of the string should be set, and the rest of the string is the list of set commands with their parameters, if any. Once you've set the EXINIT environment variable, you must export it so that vi will see it when it starts.

Suppose that you want to show the current insert mode, you want tab stops to be set to 4, and you want line numbering turned on. Using the EXINIT environment variable, you could do so with the following commands:

EXINIT='set showmode tab stop=4 number'

export EXINIT

Please note: In this example, we used the Korn shell, which is the most standard shell. The Bourne shell uses the same syntax. If you're using the C shell, you would instead use the single command:

setenv EXINIT 'set showmode tab stop=4 number'

The .exrc file

If you want your default vi configuration to have predefined macros, you'll need to use the .exrc file instead of the EXINIT environment variable. The .exrc file can contain comments, colon commands (those entered on the last line of the vi editor and beginning with :, such as :set showmode), and macro definitions.

Comments in the .exrc file are those lines that begin with a " character (double-quote character). vi ignores all characters after the ". You can include as many comments in the .exrc file as you like. One warning-don't put any blank lines in the .exrc file. vi doesn't care for blank lines in the configuration file very much and may ignore your configuration options.

A sample .exrc file

Now let's put all of this information together into a single example. Figure A shows an .exrc file that sets up some features that make vi very easy to use.

Figure A

" Issue the 'showmode' command to tell vi to display the current editing mode

:set showmode

"

" Block messages from other users to keep my display clean

:set nomesg

"

" Tell vi to also read the .exrc file in the current directory for further customization

:set exrc

"

" Define F1 to show a customized "help" file

:map #1 :!more ~/.vi_help^M

"

" Define F2 to save current changes to file

" (uses the current filename)

:map #2 :w^M

"

" Define F3 to display line numbers on screen

:map #3 :set number^M

"

" Define F4 to remove line numbers from screen

:map #4 :set nonumber^M

"

" Define F5 to prepare for programming by turning on the autoindenting feature and setting the tab stops to 4

:map #5 :set autoindent^M:set tab stop=4^M

"

" Define F6 to set the tab stops to 4 characters

:map #5 :set tab stop=4^M

"

" Define F10 to display "long help" for vi

:map #0 :!man vi^M

"

" Set some useful abbreviations

:ab s4 Solaris v2.4

:ab s4x Solaris v2.4 for x86

:ab s5 Solaris v2.5

:ab s5x Solaris v2.5 for x86

Let's examine this .exrc file to see just what it does for us. The first three commands shouldn't be any surprise, as we covered them already. They simply tell vi to show us which mode we're in, to block messages from external sources, and to allow further customization in .exrc files found in the current directory.

In our next command, we map a command to [F1]. After the :map #1 portion of the first command, you see the character sequence :!more ~/.vi_help^M. If youíre familiar with vi, you may know that the:! sequence allows you to run any UNIX command while youíre in the vi editor. For instance, while you're in command mode in vi, you can type :!ls -al (followed by the [Enter] key) to get a long listing of all files in your current directory.

This is a great feature because you don't have to exit and re-enter vi just to run the ls ­al command, saving you a lot of keystrokes. We simply created a file in our home directory named .vi_help that we use to hold some quick notes on using our customized version of vi, as well as some frequently-used commands. Our .vi_help file is shown in Figure B. Now, by pressing [F1] we can call up this file to the screen to refresh our memories.

Figure B

**************************** Short Help for Vi ****************************

F1-Short help, F2-Write File, F3-Show Line #s, F4-Hide Line #s,

F5-Autoindent ON+Set Tabs=4, F10-Long help

s4 -> Solaris v2.4

s4x -> Solaris v2.4 for x86

s5 -> Solaris v2.5

s5x -> Solaris v2.5 for x86
Delete: x=curr char, d$=del to end of line

dd=current line, #dd=next # lines

Insert: a=after curr char, i=before curr char, o=new line after this one

A=after end of line, I=at start of line, O=new line before this one

Move: 0 (zero)=move to start of line, $=move to end of line

b=back one word, w=forward one word

G=end of file, #G=goto line #

Misc: :q!=Quit, no save, :wq=Save & Quit, /xxx=search for xxx

!cmd=execute command cmd in shell

We've created a few other macros as well in this customization file. We've defined [F2] to write the file, so we don't lose any changes. [F3] and [F4] turn the line numbers on and off, respectively. We defined [F5] to turn on the autoindent mode and set tab stops to 4 to make vi more convenient for programming.

While the short help file is useful, sometimes we need to search for a command that we don't often use. That's why we defined [F10] to open the manual entry for vi. Here we can search for any command we want. To do this, we again started a shell and executed a command, only this time we executed the man vi command.

Since we write about Solaris often, we created a few abbreviations. Thus, when we write articles, we won't have to type Solaris v2.5 for x86 whenever we need to reference it. Instead, we can type s5x, and let vi do the substitution for us.

That's all there is to it

You've now seen how easy it is to customize vi to suit your preferences. While you have some limited ability to customize vi with the EXINIT file, you'll probably want to create your own .exrc file because of the extra flexibility.

Conclusion

In this article, we used the .exrc file to display the current working mode, keep other users' messages off our display, and create a set of useful macros. You, too, can make your life with the vi editor easier and more productive. You can now apply the techniques contained in this article to customize vi to include your own working preferences.

Alvin J. Alexander is an independent consultant specializing in UNIX and the Internet. He has worked on UNIX networks to support the Space Shuttle, international clients, and various Internet Service Providers. He provided UNIX and Internet training to over 400 clients in the last three years.


[Return to Index for Inside Solaris - March 1996 Issue]

Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.

Inside Solaris is a publication of The Cobb Group.
1-800-223-8720